using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Collections;
using System.Globalization;

namespace PopLava.Tools
{
    public partial class FontDialog2 : Form
    {
        private XmlDocument g_xmlDocument;
        private string g_sTileName = String.Empty;
        private string g_sCurrentViewID = String.Empty;
        private string g_sXmlType = "Current Tile";
        private string g_sType = String.Empty;
        private FontHelper fh = new FontHelper();

        public FontDialog2(string sTileName, ref XmlDocument xmlSavedSettings, string sCurrentViewID, string sType)
        {
            InitializeComponent();
            g_sTileName = sTileName;
            g_xmlDocument = xmlSavedSettings;
            g_sCurrentViewID = sCurrentViewID;
            g_sType = sType;
            this.Text = this.Text + " (" + sType + ")";
            GetSavedFont();
        }
        private void GetSavedFont()
        {
            switch (g_sXmlType)
            {
                case "Current Tile":
                    {
                        XmlNodeList xmlViews = g_xmlDocument.DocumentElement.GetElementsByTagName("view");
                        foreach (XmlNode n1 in xmlViews)
                        {
                            if (n1.FirstChild.InnerText == g_sCurrentViewID)
                            {
                                XmlNodeList xmlTiles = n1.SelectNodes("tile");
                                foreach (XmlNode n2 in xmlTiles)
                                {
                                    if (n2.FirstChild.InnerText == g_sTileName)
                                    {
                                        XmlNode n3 = null;
                                        XmlNode n4 = null;
                                        XmlNode n5 = null;
                                        XmlNode n6 = null;
                                        if (g_sType == "static")
                                        {
                                            n3 = n2.SelectSingleNode("fontfamily");
                                            n4 = n2.SelectSingleNode("fontsize");
                                            n5 = n2.SelectSingleNode("fontstyle");
                                            n6 = n2.SelectSingleNode("foregroundcolor");
                                        }
                                        else if (g_sType == "hover")
                                        {
                                            n3 = n2.SelectSingleNode("hoverfontfamily");
                                            n4 = n2.SelectSingleNode("hoverfontsize");
                                            n5 = n2.SelectSingleNode("hoverfontstyle");
                                            n6 = n2.SelectSingleNode("hoverforegroundcolor");
                                        }
                                        if (n3.InnerText.Length > 0)
                                        {
                                            this.ExampleText_Label.Font = fh.GetFont(n3.InnerText, n4.InnerText, n5.InnerText);
                                            string sForeColor = n6.InnerText;
                                            if (sForeColor.Length > 0)
                                            {
                                                string[] arrForeColors = sForeColor.Split(',');
                                                this.ExampleText_Label.ForeColor = Color.FromArgb(Convert.ToInt32(arrForeColors[0], CultureInfo.CurrentCulture), Convert.ToInt32(arrForeColors[1], CultureInfo.CurrentCulture), Convert.ToInt32(arrForeColors[2], CultureInfo.CurrentCulture), Convert.ToInt32(arrForeColors[3], CultureInfo.CurrentCulture));
                                            }
                                        }
                                        break;
                                    }
                                }
                                break;
                            }
                        }
                        break;
                    }
                case "Default For Current View":
                    {
                        XmlNodeList xmlViews = g_xmlDocument.DocumentElement.GetElementsByTagName("view");
                        foreach (XmlNode n2 in xmlViews)
                        {
                            if (n2.FirstChild.InnerText == g_sCurrentViewID)
                            {
                                XmlNode n3 = null;
                                XmlNode n4 = null;
                                XmlNode n5 = null;
                                XmlNode n6 = null;
                                if (g_sType == "static")
                                {
                                    n3 = n2.SelectSingleNode("defaultfontfamily");
                                    n4 = n2.SelectSingleNode("defaultfontsize");
                                    n5 = n2.SelectSingleNode("defaultfontstyle");
                                    n6 = n2.SelectSingleNode("defaultforegroundcolor");
                                }
                                else if (g_sType == "hover")
                                {
                                    n3 = n2.SelectSingleNode("defaulthoverfontfamily");
                                    n4 = n2.SelectSingleNode("defaulthoverfontsize");
                                    n5 = n2.SelectSingleNode("defaulthoverfontstyle");
                                    n6 = n2.SelectSingleNode("defaulthoverforegroundcolor");
                                }
                                if (n3.InnerText.Length > 0)
                                {
                                    this.ExampleText_Label.Font = fh.GetFont(n3.InnerText, n4.InnerText, n5.InnerText);
                                    string sForeColor = n6.InnerText;
                                    if (sForeColor.Length > 0)
                                    {
                                        string[] arrForeColors = sForeColor.Split(',');
                                        this.ExampleText_Label.ForeColor = Color.FromArgb(Convert.ToInt32(arrForeColors[0], CultureInfo.CurrentCulture), Convert.ToInt32(arrForeColors[1], CultureInfo.CurrentCulture), Convert.ToInt32(arrForeColors[2], CultureInfo.CurrentCulture), Convert.ToInt32(arrForeColors[3], CultureInfo.CurrentCulture));
                                    }
                                }
                            }
                        }
                        break;
                    }
                case "Default For WinClickPro (All)":
                    {
                        XmlNode n1 = g_xmlDocument.DocumentElement; // WinClickPro
                        XmlNode n2 = null;
                        XmlNode n3 = null;
                        XmlNode n4 = null;
                        XmlNode n5 = null;
                        if (g_sType == "static")
                        {
                            n2 = n1.SelectSingleNode("defaultfontfamily");
                            n3 = n1.SelectSingleNode("defaultfontsize");
                            n4 = n1.SelectSingleNode("defaultfontstyle");
                            n5 = n1.SelectSingleNode("defaultforegroundcolor");
                        }
                        else if (g_sType == "hover")
                        {
                            n2 = n1.SelectSingleNode("defaulthoverfontfamily");
                            n3 = n1.SelectSingleNode("defaulthoverfontsize");
                            n4 = n1.SelectSingleNode("defaulthoverfontstyle");
                            n5 = n1.SelectSingleNode("defaulthoverforegroundcolor");
                        }
                        if (n3.InnerText.Length > 0)
                        {
                            this.ExampleText_Label.Font = fh.GetFont(n2.InnerText, n3.InnerText, n4.InnerText);
                            string sForeColor = n5.InnerText;
                            if (sForeColor.Length > 0)
                            {
                                string[] arrForeColors = sForeColor.Split(',');
                                this.ExampleText_Label.ForeColor = Color.FromArgb(Convert.ToInt32(arrForeColors[0], CultureInfo.CurrentCulture), Convert.ToInt32(arrForeColors[1], CultureInfo.CurrentCulture), Convert.ToInt32(arrForeColors[2], CultureInfo.CurrentCulture), Convert.ToInt32(arrForeColors[3], CultureInfo.CurrentCulture));
                            }
                        }
                        break;
                    }
            }
        }
        /// <summary>
        /// Saves the font of a tile
        /// </summary>
        private void SaveTileFont()
        {
            XmlHelper xmlHelper = new XmlHelper(ref g_xmlDocument);
            XmlNode n1 = xmlHelper.GetTileNode(g_sTileName, g_sCurrentViewID);
            if (n1 != null)
            {
                XmlNode n2 = null;
                if (g_sType == "static")
                {
                    n2 = n1.SelectSingleNode("fontfamily");
                    n2.InnerText = ExampleText_Label.Font.FontFamily.Name;

                    n2 = n1.SelectSingleNode("fontsize");
                    n2.InnerText = ExampleText_Label.Font.Size.ToString();

                    n2 = n1.SelectSingleNode("fontstyle");
                    n2.InnerText = ExampleText_Label.Font.Style.ToString();

                    n2 = n1.SelectSingleNode("foregroundcolor");
                    n2.InnerText = GetAlphaChannel() + "," + ExampleText_Label.ForeColor.R.ToString(CultureInfo.InvariantCulture) + "," + ExampleText_Label.ForeColor.G.ToString(CultureInfo.InvariantCulture) + "," + ExampleText_Label.ForeColor.B.ToString(CultureInfo.InvariantCulture);
                }
                else if (g_sType == "hover")
                {
                    n2 = n1.SelectSingleNode("hoverfontfamily");
                    n2.InnerText = ExampleText_Label.Font.FontFamily.Name;

                    n2 = n1.SelectSingleNode("hoverfontsize");
                    n2.InnerText = ExampleText_Label.Font.Size.ToString();

                    n2 = n1.SelectSingleNode("hoverfontstyle");
                    n2.InnerText = ExampleText_Label.Font.Style.ToString();

                    n2 = n1.SelectSingleNode("hoverforegroundcolor");
                    n2.InnerText = GetAlphaChannel() + "," + ExampleText_Label.ForeColor.R.ToString(CultureInfo.InvariantCulture) + "," + ExampleText_Label.ForeColor.G.ToString(CultureInfo.InvariantCulture) + "," + ExampleText_Label.ForeColor.B.ToString(CultureInfo.InvariantCulture);
                }
            }
        }
        /// <summary>
        /// Saves the font a tile
        /// </summary>
        private void SaveViewFont()
        {
            XmlNodeList xmlViews = g_xmlDocument.DocumentElement.GetElementsByTagName("view");
            foreach (XmlNode n1 in xmlViews)
            {
                if (n1.FirstChild.InnerText == g_sCurrentViewID)
                {
                    XmlNode n2 = null;
                    if (g_sType == "static")
                    {
                        n2 = n1.SelectSingleNode("defaultfontfamily");
                        n2.InnerText = ExampleText_Label.Font.FontFamily.Name;

                        n2 = n1.SelectSingleNode("defaultfontsize");
                        n2.InnerText = ExampleText_Label.Font.Size.ToString();

                        n2 = n1.SelectSingleNode("defaultfontstyle");
                        n2.InnerText = ExampleText_Label.Font.Style.ToString();

                        n2 = n1.SelectSingleNode("defaultforegroundcolor");
                        n2.InnerText = GetAlphaChannel() + "," + ExampleText_Label.ForeColor.R.ToString(CultureInfo.InvariantCulture) + "," + ExampleText_Label.ForeColor.G.ToString(CultureInfo.InvariantCulture) + "," + ExampleText_Label.ForeColor.B.ToString(CultureInfo.InvariantCulture);
                    }
                    else if (g_sType == "hover")
                    {
                        n2 = n1.SelectSingleNode("defaulthoverfontfamily");
                        n2.InnerText = ExampleText_Label.Font.FontFamily.Name;

                        n2 = n1.SelectSingleNode("defaulthoverfontsize");
                        n2.InnerText = ExampleText_Label.Font.Size.ToString();

                        n2 = n1.SelectSingleNode("defaulthoverfontstyle");
                        n2.InnerText = ExampleText_Label.Font.Style.ToString();

                        n2 = n1.SelectSingleNode("defaulthoverforegroundcolor");
                        n2.InnerText = GetAlphaChannel() + "," + ExampleText_Label.ForeColor.R.ToString(CultureInfo.InvariantCulture) + "," + ExampleText_Label.ForeColor.G.ToString(CultureInfo.InvariantCulture) + "," + ExampleText_Label.ForeColor.B.ToString(CultureInfo.InvariantCulture);
                    }
                }
            }
        }
        /// <summary>
        /// Saves the font of a tile
        /// </summary>
        private void SaveWinClickProFont()
        {
            XmlNode n1 = g_xmlDocument.DocumentElement; // WinClickPro
            XmlNode n2 = null;
            if (g_sType == "static")
            {
                n2 = n1.SelectSingleNode("defaultfontfamily");
                n2.InnerText = ExampleText_Label.Font.FontFamily.Name;

                n2 = n1.SelectSingleNode("defaultfontsize");
                n2.InnerText = ExampleText_Label.Font.Size.ToString();

                n2 = n1.SelectSingleNode("defaultfontstyle");
                n2.InnerText = ExampleText_Label.Font.Style.ToString();

                n2 = n1.SelectSingleNode("defaultforegroundcolor");
                n2.InnerText = GetAlphaChannel() + "," + ExampleText_Label.ForeColor.R.ToString(CultureInfo.InvariantCulture) + "," + ExampleText_Label.ForeColor.G.ToString(CultureInfo.InvariantCulture) + "," + ExampleText_Label.ForeColor.B.ToString(CultureInfo.InvariantCulture);
            }
            else if (g_sType == "hover")
            {
                n2 = n1.SelectSingleNode("defaulthoverfontfamily");
                n2.InnerText = ExampleText_Label.Font.FontFamily.Name;

                n2 = n1.SelectSingleNode("defaulthoverfontsize");
                n2.InnerText = ExampleText_Label.Font.Size.ToString();

                n2 = n1.SelectSingleNode("defaulthoverfontstyle");
                n2.InnerText = ExampleText_Label.Font.Style.ToString();

                n2 = n1.SelectSingleNode("defaulthoverforegroundcolor");
                n2.InnerText = GetAlphaChannel() + "," + ExampleText_Label.ForeColor.R.ToString(CultureInfo.InvariantCulture) + "," + ExampleText_Label.ForeColor.G.ToString(CultureInfo.InvariantCulture) + "," + ExampleText_Label.ForeColor.B.ToString(CultureInfo.InvariantCulture);
            }
        }
        /// <summary>
        /// Opens a color picker and saves the users color choice
        /// </summary>
        private void FontChange_Button_Click(object sender, EventArgs e)
        {
            FontDialog fd = new FontDialog();
            fd.FontMustExist = true;
            fd.AllowSimulations = false;
            fd.AllowVectorFonts = false;
            fd.AllowVerticalFonts = false;
            fd.ShowColor = true;
            fd.AllowScriptChange = true;
            fd.Font = ExampleText_Label.Font;
            fd.Color = ExampleText_Label.ForeColor;
            if (fd.ShowDialog() == DialogResult.OK)
            {
                this.ExampleText_Label.Font = fd.Font;
                this.ExampleText_Label.ForeColor = fd.Color;
            }
            switch (g_sXmlType)
            {
                case "Current Tile":
                    {
                        SaveTileFont();
                        break;
                    }
                case "Default For Current View":
                    {
                        SaveViewFont();
                        break;
                    }
                case "Default For WinClickPro (All)":
                    {
                        SaveWinClickProFont();
                        break;
                    }
            }
        }
        /// <summary>
        /// Restores the value to it's parent default.
        /// </summary>
        private void FontRestore_Button_Click(object sender, EventArgs e)
        {
            switch (g_sXmlType)
            {
                case "Current Tile":
                    {
                        RestoreTileFont();
                        SaveTileFont();
                        break;
                    }
                case "Default For Current View":
                    {
                        RestoreViewFont();
                        SaveViewFont();
                        break;
                    }
                case "Default For WinClickPro (All)":
                    {
                        RestoreRootFont();
                        SaveWinClickProFont();
                        break;
                    }
            }
        }
        /// <summary>
        /// Restores the tile to the view default
        /// </summary>
        private void RestoreTileFont()
        {
            XmlNodeList xmlViews = g_xmlDocument.DocumentElement.GetElementsByTagName("view");
            foreach (XmlNode n1 in xmlViews)
            {
                if (n1.FirstChild.InnerText == g_sCurrentViewID)
                {
                    XmlNode n2 = null;
                    XmlNode n3 = null;
                    XmlNode n4 = null;
                    XmlNode n5 = null;

                    if (g_sType == "static")
                    {
                        n2 = n1.SelectSingleNode("defaultfontfamily");
                        n3 = n1.SelectSingleNode("defaultfontsize");
                        n4 = n1.SelectSingleNode("defaultfontstyle");
                        n5 = n1.SelectSingleNode("defaultforegroundcolor");
                    }
                    else if (g_sType == "hover")
                    {
                        n2 = n1.SelectSingleNode("defaulthoverfontfamily");
                        n3 = n1.SelectSingleNode("defaulthoverfontsize");
                        n4 = n1.SelectSingleNode("defaulthoverfontstyle");
                        n5 = n1.SelectSingleNode("defaulthoverforegroundcolor");
                    }
                    XmlNodeList xmlTiles = n1.SelectNodes("tile");
                    foreach (XmlNode n6 in xmlTiles)
                    {
                        if (n6.FirstChild.InnerText == g_sTileName)
                        {
                            XmlNode n7 = null;
                            XmlNode n8 = null;
                            XmlNode n9 = null;
                            XmlNode n10 = null;
                            if (g_sType == "static")
                            {
                                n7 = n6.SelectSingleNode("fontfamily");
                                n8 = n6.SelectSingleNode("fontsize");
                                n9 = n6.SelectSingleNode("fontstyle");
                                n10 = n6.SelectSingleNode("foregroundcolor");
                            }
                            else if (g_sType == "hover")
                            {
                                n7 = n6.SelectSingleNode("hoverfontfamily");
                                n8 = n6.SelectSingleNode("hoverfontsize");
                                n9 = n6.SelectSingleNode("hoverfontstyle");
                                n10 = n6.SelectSingleNode("hoverforegroundcolor");
                            }
                            n7.InnerText = n2.InnerText;
                            n8.InnerText = n3.InnerText;
                            n9.InnerText = n4.InnerText;
                            n10.InnerText = n5.InnerText;
                            this.ExampleText_Label.Font = fh.GetFont(n7.InnerText, n8.InnerText, n9.InnerText);
                            string sForeColor = n10.InnerText;
                            if (sForeColor.Length > 0)
                            {
                                string[] arrForeColors = sForeColor.Split(',');
                                this.ExampleText_Label.ForeColor = Color.FromArgb(Convert.ToInt32(arrForeColors[0], CultureInfo.CurrentCulture), Convert.ToInt32(arrForeColors[1], CultureInfo.CurrentCulture), Convert.ToInt32(arrForeColors[2], CultureInfo.CurrentCulture), Convert.ToInt32(arrForeColors[3], CultureInfo.CurrentCulture));
                            }
                            break;
                        }
                    }
                    break;
                }
            }
        }
        /// <summary>
        /// Restores the tile to the view default
        /// </summary>
        private void RestoreViewFont()
        {
            XmlNode n1 = g_xmlDocument.DocumentElement;
            XmlNode n2 = null;
            XmlNode n3 = null;
            XmlNode n4 = null;
            XmlNode n5= null;
            if (g_sType == "static")
            {
                n2 = n1.SelectSingleNode("defaultfontfamily");
                n3 = n1.SelectSingleNode("defaultfontsize");
                n4 = n1.SelectSingleNode("defaultfontstyle");
                n5 = n1.SelectSingleNode("defaultforeground");
            }
            else if (g_sType == "hover")
            {
                n2 = n1.SelectSingleNode("defaulthoverfontfamily");
                n3 = n1.SelectSingleNode("defaulthoverfontsize");
                n4 = n1.SelectSingleNode("defaulthoverfontstyle");
                n5 = n1.SelectSingleNode("defaulthoverforeground");
            }

            XmlNodeList xmlViews = g_xmlDocument.DocumentElement.GetElementsByTagName("view");
            foreach (XmlNode n6 in xmlViews)
            {
                if (n6.FirstChild.InnerText == g_sCurrentViewID)
                {
                    XmlNode n7 = null;
                    XmlNode n8 = null;
                    XmlNode n9 = null;
                    XmlNode n10 = null;
                    if (g_sType == "static")
                    {
                        n7 = n1.SelectSingleNode("defaultfontfamily");
                        n8 = n1.SelectSingleNode("defaultfontsize");
                        n9 = n1.SelectSingleNode("defaultfontstyle");
                        n10 = n1.SelectSingleNode("defaultforeground");
                    }
                    else if (g_sType == "hover")
                    {
                        n7 = n1.SelectSingleNode("defaulthoverfontfamily");
                        n8 = n1.SelectSingleNode("defaulthoverfontsize");
                        n9 = n1.SelectSingleNode("defaulthoverfontstyle");
                        n10 = n1.SelectSingleNode("defaulthoverforeground");
                    }
                    n7.InnerText = n2.InnerText;
                    n8.InnerText = n3.InnerText;
                    n9.InnerText = n4.InnerText;
                    n10.InnerText = n5.InnerText;
                    this.ExampleText_Label.Font = fh.GetFont(n7.InnerText, n8.InnerText, n9.InnerText);
                    string sForeColor = n10.InnerText;
                    if (sForeColor.Length > 0)
                    {
                        string[] arrForeColors = sForeColor.Split(',');
                        this.ExampleText_Label.ForeColor = Color.FromArgb(Convert.ToInt32(arrForeColors[0], CultureInfo.CurrentCulture), Convert.ToInt32(arrForeColors[1], CultureInfo.CurrentCulture), Convert.ToInt32(arrForeColors[2], CultureInfo.CurrentCulture), Convert.ToInt32(arrForeColors[3], CultureInfo.CurrentCulture));
                    }
                    break;
                }
            }
        }
        /// <summary>
        /// Restores the tile to the view default
        /// </summary>
        private void RestoreRootFont()
        {
            XmlNode n1 = g_xmlDocument.DocumentElement; // WinClickPro
            XmlNode n2 = null;
            XmlNode n3 = null;
            XmlNode n4 = null;
            XmlNode n5 = null;
            if (g_sType == "static")
            {
                n2 = n1.SelectSingleNode("defaultfontfamily");
                n3 = n1.SelectSingleNode("defaultfontsize");
                n4 = n1.SelectSingleNode("defaultfontstyle");
                n5 = n1.SelectSingleNode("defaultforeground");
            }
            else if (g_sType == "hover")
            {
                n2 = n1.SelectSingleNode("defaulthoverfontfamily");
                n3 = n1.SelectSingleNode("defaulthoverfontsize");
                n4 = n1.SelectSingleNode("defaulthoverfontstyle");
                n5 = n1.SelectSingleNode("defaulthoverforeground");
            }
            n2.InnerText = "";
            n3.InnerText = "";
            n4.InnerText = "";
            n5.InnerText = "";
            string sForeColor = n5.InnerText;
            if (sForeColor.Length > 0)
            {
                string[] arrForeColors = sForeColor.Split(',');
                this.ExampleText_Label.ForeColor = Color.FromArgb(Convert.ToInt32(arrForeColors[0], CultureInfo.CurrentCulture), Convert.ToInt32(arrForeColors[1], CultureInfo.CurrentCulture), Convert.ToInt32(arrForeColors[2], CultureInfo.CurrentCulture), Convert.ToInt32(arrForeColors[3], CultureInfo.CurrentCulture));
            }
        }
        /// <summary>
        /// Set the type (tile, view, all)
        /// </summary>
        private void Tile_RadioButton_Click(object sender, EventArgs e)
        {
            g_sXmlType = "Current Tile";
            GetSavedFont();
        }
        /// Set the type (tile, view, all)
        private void View_RadioButton_Click(object sender, EventArgs e)
        {
            g_sXmlType = "Default For Current View";
            GetSavedFont();
        }
        /// Set the type (tile, view, all)
        private void WinClickPro_RadioButton_Click(object sender, EventArgs e)
        {
            g_sXmlType = "Default For WinClickPro (All)";
            GetSavedFont();
        }
        /// <summary>
        /// Returns 255 or 0 depending on the backcolor of the current tile
        /// </summary>
        private string GetAlphaChannel()
        {
            string sAlphaChannel = String.Empty;
            if (this.ExampleText_Label.ForeColor.Name == "Transparent")
            {
                sAlphaChannel = "0";
            }
            else
            {
                sAlphaChannel = "255";
            }
            return sAlphaChannel;
        }
    }
}